route.ts 1.0 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344
  1. import { getExpense, updateExpense, deleteExpense } from "@/lib/db/store";
  2. export async function GET(
  3. req: Request,
  4. { params }: { params: Promise<{ id: string }> },
  5. ) {
  6. const { id } = await params;
  7. const expense = await getExpense(id);
  8. if (!expense) {
  9. return Response.json({ error: "Expense not found" }, { status: 404 });
  10. }
  11. return Response.json(expense);
  12. }
  13. export async function PATCH(
  14. req: Request,
  15. { params }: { params: Promise<{ id: string }> },
  16. ) {
  17. const { id } = await params;
  18. const body = await req.json();
  19. const expense = await updateExpense(id, body);
  20. if (!expense) {
  21. return Response.json({ error: "Expense not found" }, { status: 404 });
  22. }
  23. return Response.json(expense);
  24. }
  25. export async function DELETE(
  26. req: Request,
  27. { params }: { params: Promise<{ id: string }> },
  28. ) {
  29. const { id } = await params;
  30. const deleted = await deleteExpense(id);
  31. if (!deleted) {
  32. return Response.json({ error: "Expense not found" }, { status: 404 });
  33. }
  34. return new Response(null, { status: 204 });
  35. }